﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ECOSYSTEM_Dominio;

namespace ECOSYSTEM_DAL
{
    public class TemporalDAL
    {
        SqlConnection cnn;
        String Fecha;
        public Boolean guardarTemporal(Temporal elTemporal) {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("RegistrarTemporal", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@IGV", SqlDbType.Float);
                cmd.Parameters.Add("@TCambio", SqlDbType.Float);
                cmd.Parameters.Add("@MonedaVenta", SqlDbType.NVarChar,10);
                cmd.Parameters.Add("@Fecha", SqlDbType.NVarChar,10);
                cmd.Parameters["@IGV"].Value = elTemporal.IGV;
                cmd.Parameters["@TCambio"].Value = elTemporal.TasaCambio;
                cmd.Parameters["@MonedaVenta"].Value = elTemporal.MonedaVentas;
                cmd.Parameters["@Fecha"].Value = elTemporal.Fecha;
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally {
                if (cnn.State == ConnectionState.Open) {
                    cnn.Close();
                }
            }
            return true;
        }
        public Boolean ActualizarTemporal() {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("ActualizarTemporal", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                String Nuevafecha = DateTime.Now.ToShortDateString();
                cmd.Parameters.AddWithValue("Fecha",Nuevafecha);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return true;
        }
        public List<Temporal> ObtenerActualesDatos()
        {
            List<Temporal> lista;
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("ObtenerActualesDatos", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                lista = new List<Temporal>();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Temporal elTemporal = new Temporal();
                    elTemporal.IGV = Convert.ToDouble(dr["IGV"]);
                    elTemporal.TasaCambio = Convert.ToDouble(dr["TCambio"]);
                    elTemporal.MonedaVentas = dr["MonedaParaVenta"].ToString();
                    lista.Add(elTemporal);
                }
                return lista;
            }
            catch (SqlException e) { throw new Exception(e.Message); }
            catch (Exception ex) { throw new Exception(ex.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
        }
        public String ObtenerFecha() {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("ObtenerFecha", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Fecha = dr["Fecha"].ToString(); ;
                }
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return Fecha;
        }
    }
}
